<?php
class Zen_View_Helper_Tiny_Widget extends Zend_View_Helper_FormElement {
	
	public $rows = 24;

    public $cols = 80;
	
	public function tiny_Widget($name, $value = null, $attribs = null) {
		
		$info = $this->_getInfo($name, $value, $attribs);
        extract($info); 

        $disabled = '';
        if ($disable) {
            $disabled = ' disabled="disabled"';
        }
		
        $attribs['style'] = (isset($attribs['style'])) ? $attribs['style'] : '';
        $attribs['style'] .= ' width: 100%'; 
        
        if (empty($attribs['rows'])) {
            $params['rows'] = (int) $this->rows;
        }
        if (empty($attribs['cols'])) {
            $attribs['cols'] = (int) $this->cols;
        }
        
        $xhtml = '<textarea name="' . $this->view->escape($name) . '"'
                . ' id="' . $this->view->escape($name) . '"'
                . $disabled
                . $this->_htmlAttribs($attribs) . '>'
                . $this->view->escape($value) . '</textarea>';
                
		$tiny = User_Model_Private::getParam('tiny',1);
		if(!$tiny) return 	$xhtml;

        
        Zen_Script::add('/cms/res/tiny_mce/jquery.tinymce.js',0,0,true);
        Zen_Script::add('/cms/res/tiny_mce/tiny_mce.js',0,0,true);
              
        $js = "
				<script type='text/javascript'>
				$('#$name').tinymce({
					// General options
					mode : 'textareas',
					theme : 'advanced',
					height : '400',
					language : 'ru',
					convert_urls : false,
					plugins : 'codeprotect,safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template',
					php_external_list_url : '/cms/res/tinymce/jscripts/tiny_mce/plugins/php/examples/example_php_list.js',
						// Theme options
					php_external_list_url : '/cms/res/tinymce/jscripts/tiny_mce/plugins/php/examples/example_php_list.js',
					theme_advanced_buttons1 : 'bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect',
					theme_advanced_buttons2 : 'cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor',
					theme_advanced_buttons3 : 'tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen',
					theme_advanced_buttons4 : 'insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage',
						theme_advanced_toolbar_location : 'top',
						theme_advanced_toolbar_align : 'left',
						theme_advanced_statusbar_location : 'bottom',
						//theme_advanced_resizing : true,
						// Schema is HTML5 instead of default HTML4
						schema: 'html5',
						// End container block element when pressing enter inside an empty block
						end_container_on_empty_block: true,
						// HTML5 formats
						style_formats : [
							{title : 'h1', block : 'h1'},
							{title : 'h2', block : 'h2'},
							{title : 'h3', block : 'h3'},
							{title : 'h4', block : 'h4'},
							{title : 'h5', block : 'h5'},
							{title : 'h6', block : 'h6'},
							{title : 'p', block : 'p'},
							{title : 'div', block : 'div'},
							{title : 'pre', block : 'pre'},
							{title : 'section', block : 'section', wrapper: true, merge_siblings: false},
							{title : 'article', block : 'article', wrapper: true, merge_siblings: false},
							{title : 'blockquote', block : 'blockquote', wrapper: true},
							{title : 'hgroup', block : 'hgroup', wrapper: true},
							{title : 'aside', block : 'aside', wrapper: true},
							{title : 'figure', block : 'figure', wrapper: true}
						],
						force_p_newlines : false,
						force_br_newlines : true,
						extended_valid_elements : 'script[type|src],style[type|src],div[*],p[*],iframe[name|src|framespacing|border|frameborder|scrolling|title|height|width],object[declare|classid|codebase|data|type|codetype|archive|standby|height|width|usemap|name|tabindex|align|border|hspace|vspace]',
						forced_root_block : '',
						content_css : '/site/theme/css/reset.css,/site/theme/css/fonts.css,/site/theme/css/template.css,/site/theme/css/pages.css,/site/theme/css/tiny.css',
						file_browser_callback : function(field_name, url, type, win) {
					      var w = window.open('/backend/elfinder/admin/index/', null, 'width=600,height=500');
					       w.tinymceFileField = field_name;
					       w.tinymceFileWin = win;
					   }

						
					});
				</script>
        "; 
        
        return  $xhtml.$js;
		
	}
	
}